package com.itheima.HDFS;


import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;

import java.io.FileInputStream;
import java.net.URI;

public class HDFSTest {

    //获取HDFS客户端
    @Test
    public void test1() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fileSystem = FileSystem.get(conf);
        System.out.println("------------------------------");

        conf=new Configuration();
        conf.set("fs.defaultFS","hdfs://node1:8020");
        FileSystem fileSystem1 = FileSystem.get(conf);
        System.out.println(fileSystem1);

        //模拟root用户，伪装用户
        URI uri = new URI("hdfs://node1:8020");
        FileSystem fs2 = FileSystem.get(uri, conf, "root");
        System.out.println(fs2);


        uri = new URI("hdfs://node1:8020");
        FileSystem fs3 = FileSystem.get(uri, conf);
        System.out.println(fs3);

        //newInstance获取文件系统对象，和get一模一样
    }


    //获取某个路径下的所有文件
    @Test
    public void test2() throws Exception{
        //1、获取HDFS客户端对象FileSystem对象
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://node1:8020"), new Configuration(), "root");

        //2、执行相关操作
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/aaa"), false);//递归获取
        while (files.hasNext()){
            LocatedFileStatus fileStatus = files.next();
            System.out.println(fileStatus.getPath().getName());
        }
        //3、关闭资源
        fs.close();
    }

    //创建一个文件夹
    @Test
    public void test3() throws Exception{
        //1、获取HDFS客户端对象FileSystem对象
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://node1:8020"), new Configuration(), "root");

        //2、执行相关操作
        boolean bbb = fs.mkdirs(new Path("/test/aaa/bbb"));
        System.out.println(bbb);

        //3、关闭资源
        fs.close();
    }

    //创建一个文件
    @Test
    public void test4() throws Exception{
        //1、获取HDFS客户端对象FileSystem对象
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://node1:8020");
        FileSystem fs = FileSystem.newInstance(conf);

        //2、执行相关操作
        FSDataOutputStream fsDataOutputStream = fs.create(new Path("/test/hello.java"));
        fsDataOutputStream.write("柳淇，你好吗".getBytes());
        fsDataOutputStream.flush();

        //3、关闭资源
        fsDataOutputStream.close();
        fs.close();
    }


    //文件下载
    @Test
    public void test6() throws Exception{
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://node1:8020"), new Configuration(), "root");

        fs.copyToLocalFile(new Path("/test/hello.java"),new Path("C:\\Users\\86186\\Desktop"));

        fs.close();
    }

    //文件上传
    @Test
    public void test7() throws Exception{
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://node1:8020"), new Configuration(), "root");

        fs.copyFromLocalFile(new Path("C:\\Users\\86186\\Desktop\\study.txt"),new Path("/test/aaa"));

        //fs.moveFromLocalFile(new Path("sdf"),new Path("dsf"));//删除本地
        fs.close();
    }


    //合并文件上传
    @Test
    public void test8() throws Exception{
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://node1:8020"), new Configuration(), "root");

        FSDataOutputStream output = fs.create(new Path("/merge.txt"));

        LocalFileSystem localfs = FileSystem.getLocal(new Configuration());
        RemoteIterator<LocatedFileStatus> files = localfs.listFiles(new Path("C:\\Users\\86186\\Desktop\\merge"), false);
        while (files.hasNext()){
            LocatedFileStatus file = files.next();
            Path path = file.getPath();
            FSDataInputStream fileInputStream = localfs.open(path);
            IOUtils.copy(fileInputStream,output);
            IOUtils.closeQuietly(fileInputStream);
        }

        output.flush();
        output.close();
        localfs.close();
        fs.close();
    }
}
